package metier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DB.Request;
import bean.Information;

public class InformationMetier {

	private static InformationMetier singleton = new InformationMetier();
	
	public static InformationMetier getInstance(){
		return singleton;
	}
	
	public Information save(Information information){
		
		if(information != null){
			String query;
			if (information.getId_information() != null) {
				//UPDATE
				query = "UPDATE information SET texte1=?,texte2=? WHERE id_information="+information.getId_information();
			} else {
				//INSERT
				query = "INSERT INTO information(texte1,texte2) VALUES (?,?)";
			}
			List<Object> param = new ArrayList<Object>();
			param.add(information.getTexte1());
			param.add(information.getTexte2());
			Request.getInstance().executeUpdate(query, param);
		}
		
		if (information.getId_information() != null) {
			return getById(information.getId_information());
		} else {
			return getById(getLastId());
		}
	}
	
	public void delete(Information information){
		String query = "DELETE FROM information WHERE id_information =" + information.getId_information();
		Request.getInstance().executeUpdate(query);
	}
	
	public Information getById(Integer id){
		Information information = null;
		String query = "SELECT * FROM information WHERE id_information=" + id;
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				information = new Information(id, resultat.getString("texte1"), resultat.getString("texte2"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return information;
	}
	
	public List<Information> getAll(){
		List<Information> informations = new ArrayList<Information>();
		String query = "SELECT * FROM information WHERE id_information";
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				Information information = new Information(resultat.getInt("id_information"), resultat.getString("texte1"), resultat.getString("texte2"));
				informations.add(information);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return informations;
	}
	public Information getLast()
	{
		Information informations = null;
		String query = "SELECT id_information FROM information WHERE id_information = (SELECT max(id_information) FROM information)";
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				informations = new Information(resultat.getInt("id_information"), resultat.getString("texte1"), resultat.getString("texte2"));
				//informations.add(information);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return informations;
	}
	private Integer getLastId(){
		Integer result = null;
		String query = "SELECT max(id_information) AS id_information FROM information";
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				result = resultat.getInt("id_information");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
